package com.qdsgjsfk.vision.ui;

import android.text.TextUtils;

import com.qdsgjsfk.vision.R;
import com.rest.business.RestProxy;
import com.rest.client.RestClient;
import com.rest.constant.Constant;
import com.rest.response.LoginResponse;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import jz.joyoung.robot.util.LogUtil;
import com.qdsgjsfk.vision.base.BaseActivity;
import com.qdsgjsfk.vision.base.BaseApplication;
import util.PreferencesHelper;

/**
 * Created by HP on 2019/9/26.
 */

public class LoadingActivity extends BaseActivity {
    @Override
    protected void initViews() {
//        if(TextUtils.isEmpty(PreferencesHelper.getInstance().getString(this,"token"))){
//        }else{
//            RestClient.getInstance().token = PreferencesHelper.getInstance().getString(this,"token");
//        }
//        finish();
    }

    @Override
    protected void initData() {
        LogUtil.e( "domain-------", PreferencesHelper.getInstance().getString( LoadingActivity.this, "domain" ) );
        //打开app，设置为医师筛查模式
        PreferencesHelper.getInstance().set( this, "model", 1 );//医生1
        if (TextUtils.isEmpty( PreferencesHelper.getInstance().getString( LoadingActivity.this, "domain" ) )) {
            startActivity( LoginActivity.class );
            finish();
        } else {
            Constant.USER_HOST = PreferencesHelper.getInstance().getString( LoadingActivity.this, "domain" );
            login();
        }
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_loading;
    }

    private void login() {
        RestProxy.getInstance().login( PreferencesHelper.getInstance().getString( this, "username" ), PreferencesHelper.getInstance().getString( this, "password" ), new Observer<LoginResponse>() {
            @Override
            public void onSubscribe(Disposable d) {
                BaseApplication.permission = false;
                BaseApplication.addUnChina = false;
                BaseApplication.addChinaNoId = false;
            }

            @Override
            public void onNext(LoginResponse loginResponse) {
                PreferencesHelper.getInstance().set( LoadingActivity.this, "token", loginResponse.data.token );
//                PreferencesHelper.getInstance().set(LoginActivity.this, "userId", loginResponse.data.userId);
//                PreferencesHelper.getInstance().set(LoginActivity.this, "perm", loginResponse.data.perm);
                RestClient.getInstance().token = loginResponse.data.token;
                startActivity( TypeActivity.class );
                for (String s : loginResponse.data.permission) {
                    System.out.println( s );
                    if (s.equals( "1002" )) {
                        BaseApplication.permission = true;
                    }
                    if (s.equals( "1150" )) {
                        BaseApplication.addUnChina = true;
                    }
                    if (s.equals( "1149" )) {
                        BaseApplication.addChinaNoId = true;
                    }
                    if (s.equals( "1156" )) {
                        BaseApplication.faceAdd = true;
                    }
                    if (s.equals( "1157" )) {
                        BaseApplication.faceSearch = true;
                    }
                }
                finish();
            }

            @Override
            public void onError(Throwable e) {
                startActivity( LoginActivity.class );
            }

            @Override
            public void onComplete() {

            }
        } );
    }
}
